﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;

namespace app.Api
{
    /*
      
    */
    public class ApiGSXT : ApiBase
    {
        public ApiGSXT(Controller ctx) : base(ctx)
        {
        }

        public void Do(string action)
        {
            GSXT ph = G.DB.Get<GSXT>(new Cd().And("selected = 1").Render()).GetFirstEntity();
            switch (action)
            {
                case "add":
                    {
                        string url = GetStringParam(new ReqStringParamOption { Name = "url" });
                        string apicode = GetStringParam(new ReqStringParamOption { Name = "apicode" });
                        GSXT p = new GSXT { url = url, apicode = apicode, selected = 0 };
                        int r = G.DB.Insert<GSXT>(p);
                        ResponseJson(new BooleanResult { value = r > 0 });
                        break;
                    }
                case "get_list":
                    {
                        ResponseJson(new ListResult<GSXT> { list = G.DB.Get<GSXT>().GetFirstTableList() });
                        break;
                    }
                case "select":
                    {
                        Cd cd = new Cd();
                        cd.And("selected = 1");
                        GSXT p = G.DB.Get<GSXT>(cd.Render()).GetFirstEntity();
                        if(p != null)
                        {
                            p.selected = 0;
                            G.DB.Update<GSXT>(p);
                        }
                        p = G.DB.GetById<GSXT>(req_id);
                        p.selected = 1;
                        ResponseJson(new BooleanResult { value = G.DB.Update<GSXT>(p) > 0 });
                        GSXTHelper.UpdateConfig();
                        break;
                    }
                case "modify":
                    {
                        string url = GetStringParam(new ReqStringParamOption { Name = "url" });
                        string apicode = GetStringParam(new ReqStringParamOption { Name = "apicode" });
                        GSXT p = new GSXT { url = url, apicode = apicode, id = req_id };
                        int r = G.DB.Update<GSXT>(p);
                        ResponseJson(new BooleanResult { value = r > 0 });
                        break;
                    }
                case "delete":
                    {
                        ResponseJson(new BooleanResult { value = G.DB.Delete<GSXT>(req_id) > 0 });
                        break;
                    }
            }
        }
    }
}